/**
 *
 *
 *@Auther z.b
 *@date 2016/9/9 13:23
 *@Email:2545375@qq.com
 *@since 1.0
 *
 */
import {REQUEST_POST,SELECT_LIST,REQUEST_GET,RECEIVE_GET} from '../actions/disAction'

const start={
    baseUrl:'/api/',
    list:null,
    loading:false,
}

const listReduce=(state={state:start},action)=>{
    switch(action.type){
        case REQUEST_GET:
            return Object.assign({},state,action);
        case RECEIVE_GET:
            return merged(state,action);
        default:
            return state;
    }
}

const merged=(...arg)=> {
    var obj = {};
    for (let i=0;i<arg.length;i++) {

        for (let key in arg[i]) {
            let curObj = arg[i][key];

            if (isJson(curObj)) {
                if (isJson(obj[key])) {
                    obj[key] = merged(obj[key], curObj); // obj 此属性已经是对象，则和该对象原来的属性合并
                } else {
                    obj[key] =merged(curObj); // obj 此属性不是对象，则和该对象原来的属性合并
                }
            } else {
                obj[key] = curObj; //属性不是obj
            }
        }

    }
    return obj;
}

/*
    判断对象是不是Json
*/
const isJson = (obj) => {
    return typeof(obj) == 'object' && Object.prototype.toString.call(obj).toLowerCase() === '[object object]' && !obj.length; //true 是 false不是
}

/*
    判断对象是不是数组
*/
const isArray = (arr) => {
    return Object.prototype.toString.call(arr).toLowerCase() === '[object array]'; //true 是 false不是
}

export {listReduce}
